package com.dlc.felear.xiaoensale.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.dlc.felear.xiaoensale.R;
import com.dlc.felear.xiaoensale.constant.Constant;
import com.dlc.felear.xiaoensale.customView.ConfirmDialog;
import com.dlc.felear.xiaoensale.dao.CookingMenuDao;
import com.dlc.felear.xiaoensale.entity.CookingMenu;
import com.dlc.felear.xiaoensale.helper.CookingMenuMangerHelper;
import com.dlc.felear.xiaoensale.utils.MenuPicUtil;
import com.dlc.felear.xiaoensale.utils.UuidUtil;

import java.util.ArrayList;
import java.util.List;


public class EmnuStdListtemAdapter extends BaseAdapter implements ConfirmDialog.ConfirmDialogCallback{

    private static final String TAG = "EmnuListtemAdapter";
    // 上下文
    private Context context;
    // 数据
    private List<CookingMenu> items;
    private MenuPicUtil menuPicUtil;
    private ConfirmDialog confirmDialog;
    private CookingMenuDao cookingMenuDao;
    private CookingMenuMangerHelper cookingMenuMangerHelper;

    public EmnuStdListtemAdapter(Context context) {
        this.context = context;
        menuPicUtil=MenuPicUtil.getInstance(context);
        items = new ArrayList<>();
        cookingMenuDao=new CookingMenuDao(context);
        confirmDialog=new ConfirmDialog(context,this);
        cookingMenuMangerHelper=CookingMenuMangerHelper.getInstance(context,cookingMenuDao);
    }

    public List<CookingMenu> getData() {
        return items;
    }


    public void setItems(List<CookingMenu> data) {

        if (data==null||data.isEmpty()) {
            items.clear();
            notifyDataSetChanged();
            Toast.makeText(context,"没对应分类菜谱",Toast.LENGTH_SHORT).show();
            return;
        }
        for(CookingMenu cookingMenu: data){
            if(cookingMenu.getType()!=1){
                data.remove(cookingMenu);
            }
        }
        items.clear();
        items.addAll(data);
        notifyDataSetChanged();
    }

    public void clearData() {
        items.clear();
        notifyDataSetChanged();
    }




    @Override
    public int getCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public CookingMenu getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(context, R.layout.adapter_std_menu_list_item, null);
            holder.tv_cookingType = convertView.findViewById(R.id.tv_cookingType);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            holder.tv_gg = convertView.findViewById(R.id.tv_gg);
            holder.iv_icon = convertView.findViewById(R.id.iv_icon);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String name=items.get(position).getName();
        holder.tv_name.setText(name);
        holder.tv_gg.setText("规格:"+items.get(position).getSpecification()+"斤");
        holder.tv_cookingType.setText("烹饪方式:"+getCookingTypeName(items.get(position).getCookingType()));
        String pic=items.get(position).getPic();
        Log.e(TAG,"name="+name+" cookingType="+items.get(position).getCookingType());
        if(TextUtils.isEmpty(pic)){
            Glide.with(context).load(R.drawable.menu_icon).into(holder.iv_icon);
        }else {
            Log.e(TAG,"pic="+pic);
            Glide.with(context).load(menuPicUtil.getLoaclpicPath(pic,items.get(position).getName())).into(holder.iv_icon);
        }
        return convertView;
    }

    private void handleClick(int p){
        confirmDialog.showDialog("确定把该菜谱添加到炒菜库",p);
    }

    private void showToast(String msg){
        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
    }

    @Override
    public void selectResult(int value, int id) {
        if(value==1){
            saveCookingMenu(items.get(id));
        }
    }
    private void saveCookingMenu(CookingMenu cookingMenu){
        if(cookingMenu==null){
          return;
        }
        List<CookingMenu> list=cookingMenuDao.queryForName(cookingMenu.getName(), Constant.MENU_NORMAL);
        if(list!=null&&!list.isEmpty()){
            showToast("该菜谱已经添加到炒菜库了");
            return;
        }
        cookingMenuDao.addNewMenu(cookingMenu, UuidUtil.getUuidByName(cookingMenu.getId()));
    }

    static class ViewHolder {
        TextView tv_name,tv_cookingType,tv_gg;
        ImageView iv_icon;
    }

    private String getCookingTypeName(int type){
        if(type==1){
            return "炒";
        }
        if(type==2){
            return "焖";
        }
        return "蒸/凉";
    }

}